﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace X3DLib
{
    public class X3dScaleRandom
    {
        private int number; //数组的大小

        public int Number
        {
            get { return number; }
            set { number = value; }
        }
        //private float size; //范围

        //public float Size
        //{
        //    get { return size; }
        //    set { size = value; }
        //}
        private float sizex; //x范围

        public float SizeX
        {
            get { return sizex; }
            set { sizex = value; }
        }
        private float sizey; //y范围

        public float SizeY
        {
            get { return sizey; }
            set { sizey = value; }
        }
        private float sizez; //z范围

        public float SizeZ
        {
            get { return sizez; }
            set { sizez = value; }
        }
        private string[] scaleArr;//设定变形数组
        /// <summary>
        /// 构造函数
        /// </summary>
        public X3dScaleRandom()
        {
            this.number = 10;
            //this.size = 1.0f;
            this.sizex = 1.0f;
            this.sizey = 1.0f;
            this.sizez = 1.0f;
            this.scaleArr = new string[this.number];
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="size">x/y/z的同一范围</param>
        /// <param name="number">数组的大小</param>
        public X3dScaleRandom(float size, int number)
        {
            this.number = number;
            //this.size = size;
            this.sizex = size;
            this.sizey = size;
            this.sizez = size;
            this.scaleArr = new string[this.number];
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sizex">x的范围</param>
        /// <param name="sizey">y的范围</param>
        /// <param name="sizez">z的范围</param>
        /// <param name="number">数组的大小</param>
        public X3dScaleRandom(float sizex,float sizey,float sizez, int number)
        {
            this.number = number;
            this.sizex = sizex;
            this.sizey = sizey;
            this.sizez = sizez;
            this.scaleArr = new string[this.number];
        }
        public string[] createScaleArr()
        {
            //scaleArr = new string[this.number];
            #region 生成scale的数组

            //可以使用两种方式初始化一个随机数发生器： 
            //第一种方法不指定随机种子，系统自动选取当前时间作为随机种子： 
            //Random ro = new Random(); 
            //第二种方法可以指定一个int型参数作为随机种子： 
            //int iSeed=10; 
            //Random ro = new Random(10);
            //这样可以保证99%不是一样。
            long tick = DateTime.Now.Ticks;
            Random rand = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));

            for (int i = 0; i < this.number; i++)
            {
                double dsx, dsy, dsz;
                //double dsx = Math.random() * (1 - 0.1) + 0.1;//比例0.1--1
                //double dsy = Math.random() * (1 - 0.1) + 0.1;
                //double dsz = Math.random() * (1 - 0.1) + 0.1;
                if (this.sizex == this.sizey && this.sizey == this.sizez)
                {
                    dsx = rand.NextDouble() * (this.sizex - 0.1) + 0.1;
                    dsy = dsz = dsx;
                }
                else
                {
                     dsx = rand.NextDouble() * (this.sizex - 0.1) + 0.1;//Math.random() * (width - 0.1) + 0.1;
                     dsy = rand.NextDouble() * (this.sizey - 0.1) + 0.1; ;//Math.random() * (width - 0.1) + 0.1;
                     dsz = rand.NextDouble() * (this.sizez - 0.1) + 0.1; ;//Math.random() * (width - 0.1) + 0.1;
                }
                //copystr = copystr + "<Transform scale='" + dsx + " " + dsy + " " + dsz + "' translation='" + dtx + " 0 " + dtz + "'>" +
                //        "\n        <Inline url='treebuild.x3d' />\n" +
                //        "</Transform>\n";
                scaleArr[i] = dsx.ToString() + " " + dsy.ToString() + " " + dsz.ToString();
            }
            #endregion
            return scaleArr;
        }
    }
}
